import pygame
import random
import sys

# 初始化pygame
pygame.init()

# 屏幕设置
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("划龙舟小游戏")

# 颜色定义
BLUE = (64, 128, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)


# 龙舟类
class DragonBoat:
    def __init__(self):
        self.width = 80
        self.height = 40
        self.x = 100
        self.y = HEIGHT // 2
        self.speed = 5
        self.score = 0
        self.time = 60

    def draw(self):
        pygame.draw.rect(screen, RED, (self.x, self.y, self.width, self.height))
        # 画龙头
        pygame.draw.polygon(screen, RED, [(self.x + self.width, self.y),
                                          (self.x + self.width + 30, self.y + self.height // 2),
                                          (self.x + self.width, self.y + self.height)])

    def move(self, keys):
        if keys[pygame.K_UP] and self.y > 0:
            self.y -= self.speed
        if keys[pygame.K_DOWN] and self.y < HEIGHT - self.height:
            self.y += self.speed
        if keys[pygame.K_LEFT] and self.x > 0:
            self.x -= self.speed
        if keys[pygame.K_RIGHT] and self.x < WIDTH - self.width:
            self.x += self.speed


# 障碍物类
class Obstacle:
    def __init__(self):
        self.width = 30
        self.height = random.randint(50, 150)
        self.x = WIDTH
        self.y = random.randint(0, HEIGHT - self.height)
        self.speed = random.randint(3, 7)

    def draw(self):
        pygame.draw.rect(screen, GREEN, (self.x, self.y, self.width, self.height))

    def move(self):
        self.x -= self.speed
        if self.x < -self.width:
            return True
        return False


# 目标类
class Target:
    def __init__(self):
        self.radius = 20
        self.x = WIDTH - 50
        self.y = random.randint(self.radius, HEIGHT - self.radius)

    def draw(self):
        pygame.draw.circle(screen, WHITE, (self.x, self.y), self.radius)


# 游戏主函数
def main():
    clock = pygame.time.Clock()
    boat = DragonBoat()
    obstacles = []
    targets = []
    font = pygame.font.SysFont(None, 36)

    # 游戏循环
    running = True
    obstacle_timer = 0
    target_timer = 0

    while running and boat.time > 0:
        # 事件处理
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        # 获取按键状态
        keys = pygame.key.get_pressed()
        boat.move(keys)

        # 生成障碍物
        obstacle_timer += 1
        if obstacle_timer >= 60:
            obstacles.append(Obstacle())
            obstacle_timer = 0

        # 生成目标
        target_timer += 1
        if target_timer >= 180 and len(targets) < 3:
            targets.append(Target())
            target_timer = 0

        # 移动障碍物
        for obstacle in obstacles[:]:
            if obstacle.move():
                obstacles.remove(obstacle)

        # 检测碰撞
        for obstacle in obstacles[:]:
            if (boat.x < obstacle.x + obstacle.width and
                    boat.x + boat.width > obstacle.x and
                    boat.y < obstacle.y + obstacle.height and
                    boat.y + boat.height > obstacle.y):
                boat.time -= 5
                obstacles.remove(obstacle)

        # 检测目标
        for target in targets[:]:
            if ((boat.x + boat.width - target.x) ** 2 + (
                    boat.y + boat.height // 2 - target.y) ** 2) <= target.radius ** 2:
                boat.score += 10
                targets.remove(target)

        # 更新时间
        boat.time -= 1 / 60

        # 绘制
        screen.fill(BLUE)
        boat.draw()
        for obstacle in obstacles:
            obstacle.draw()
        for target in targets:
            target.draw()

        # 显示分数和时间
        score_text = font.render(f"分数: {boat.score}", True, BLACK)
        time_text = font.render(f"时间: {int(boat.time)}", True, BLACK)
        screen.blit(score_text, (10, 10))
        screen.blit(time_text, (10, 50))

        pygame.display.fli
        p()
        clock.tick(60)

    # 游戏结束
    game_over_text = font.render(f"游戏结束! 最终分数: {boat.score}", True, BLACK)
    screen.blit(game_over_text, (WIDTH // 2 - 150, HEIGHT // 2))
    pygame.display.flip()
    pygame.time.wait(3000)

    pygame.quit()
    sys.exit()


if __name__ == "__main__":
    main()
